import GeewiGetUserInfoBean from '@/api/user/GeewiUserInfoBean';
import userApi from '@/api/user/GeewiUserApi';
import { useState, useCallback } from 'react';
///用户信息获取的模块model
class GeewiGetUserInfoOperate {
  userInfo?: GeewiGetUserInfoBean; ///用户信息
  setUserInfo?: (userInfo: GeewiGetUserInfoBean) => void; ///更新用户信息方法

  ///更新用户状态同时返回数据结果
  async queryUserInfoPromise(isUpgrade: boolean = true): Promise<GeewiGetUserInfoBean> {
    if (isUpgrade || this.userInfo == null) {
      //强制刷新用户信息||用户信息是空
      return userApi.geewiGetUserInfo().then(
        (data) => {
          if (this.setUserInfo != null) {
            this.setUserInfo(data);
          }
          return data;
        },
        (error) => {
          return error;
        },
      );
    } else {
      return this.userInfo;
    }
  }
}
export default function GeewiUserModel(): GeewiGetUserInfoOperate {
  let opreate = new GeewiGetUserInfoOperate();
  const [userInfo, setUserInfo] = useState<GeewiGetUserInfoBean>(new GeewiGetUserInfoBean());
  opreate.setUserInfo = setUserInfo;
  opreate.userInfo = userInfo;
  return opreate;
}
